package com.tz.tzgw.pc.web;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.util.SaResult;
import com.tz.tzgw.pc.web.annotation.AdminControllerAnnotation;
import com.tz.tzgw.pc.web.dao.*;
import com.tz.tzgw.pc.web.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

;


@Controller
@AdminControllerAnnotation
@RequestMapping("/api/")
public class SettingController {
    private final SiteDao siteDao;
    private final CompanyDao companyDao;
    private final WebcolumnDao webcolumnDao;

    private final BannerDao bannerDao;

    private final ZhaopinDao zhaopinDao;



    public SettingController(SiteDao siteDao, CompanyDao companyDao, WebcolumnDao webcolumnDao,BannerDao bannerDao,ZhaopinDao zhaopinDao) {
        this.siteDao = siteDao;
        this.companyDao = companyDao;
        this.webcolumnDao = webcolumnDao;
        this.bannerDao = bannerDao;
        this.zhaopinDao = zhaopinDao;
    }
    @RequestMapping("get_site_info")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_site_info() {
        Site site = siteDao.getInfo();
        if (site != null) {
            return SaResult.data(site);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    @RequestMapping("site_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult site_act_do(@RequestBody Site site) {
        int suc = siteDao.updateSite(site);
        if (suc  == 1) {
            return SaResult.data(site);
        } else {
            return SaResult.error("更新失败");
        }
    }


    @RequestMapping("get_company_info")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_company_info() {
        Company company = companyDao.getInfo();
        if (company != null) {
            return SaResult.data(company);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    @RequestMapping("company_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult company_act_do(@RequestBody Company company) {
        int suc = companyDao.updateSite(company);
        if (suc  == 1) {
            return SaResult.data(company);
        } else {
            return SaResult.error("更新失败");
        }
    }



    @RequestMapping("get_column_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_column_list(@RequestParam(value = "contentable", defaultValue = "0") int contentable) {
        List<Webcolumn> list = webcolumnDao.getList(contentable);
        if (list != null) {
            List<Webcolumn> tree = convertToTree(list,0l,1);
            return SaResult.data(tree);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    public static List<Webcolumn> convertToTree(List<Webcolumn> dataList, Long pid, int level) {
        List<Webcolumn> tree = new ArrayList<>();
        for (Webcolumn data : dataList) {
            if (data.getPid().equals(pid)) {
                data.setLevel(level);
                List<Webcolumn> children = convertToTree(dataList, data.getId(), level + 1);
                if (!children.isEmpty()) {
                    data.setChildren(children);
                }
                tree.add(data);
            }
        }
        return tree;
    }

    @RequestMapping("column_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult column_act_do(@RequestBody @Valid Webcolumn webcolumn, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            // 处理验证失败的情况
            return SaResult.error("参数缺失");
        }
        int suc = webcolumnDao.insertOrUpdateColumn(webcolumn);
        if (suc  == 1) {
            return SaResult.data(suc);
        } else {
            return SaResult.error("更新失败");
        }
    }


    /**
     * 后台栏目删除
     * @param id
     * @return
     */
    @RequestMapping("column_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult column_del(@RequestParam("id") Integer id) {
        int suc = webcolumnDao.deleteColumn(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }




    @RequestMapping("get_banner_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_banner_list(@RequestParam("page") Integer pageNum, @RequestParam(value = "title", defaultValue = "") String title) {
        PageResult<Banner> pageResult = bannerDao.getBannersByPageAndCondition(pageNum,20,title);
        if (pageResult != null) {
            return SaResult.data(pageResult);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    @RequestMapping("banner_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult banner_act_do(@RequestBody Banner banner) {
        int suc = bannerDao.insertOrUpdateBanner(banner);
        if (suc  == 1) {
            return SaResult.data(banner);
        } else {
            return SaResult.error("更新失败");
        }
    }

    /**
     * 后台banner删除
     * @param id
     * @return
     */
    @RequestMapping("banner_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult banner_del(@RequestParam("id") Integer id) {
        int suc = bannerDao.deleteBanner(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }



    /**
     * 后台招聘列表接口
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping("get_zhaopin_list")
    @ResponseBody
    @SaCheckLogin
    public SaResult get_zhaopin_list(@RequestParam("page") Integer pageNum, @RequestParam(value = "page_size", defaultValue = "") Integer pageSize,@RequestParam(value = "name", defaultValue = "") String name) {
        PageResult<Zhaopin> pageResult = zhaopinDao.getZhaopinByPageAndCondition(pageNum,pageSize,name);
        if (pageResult != null) {
            return SaResult.data(pageResult);
        } else {
            // 数据不存在，返回错误信息
            return SaResult.error("数据不存在");
        }
    }

    /**
     * 后台招聘增改
     * @param zhaopin
     * @return
     */
    @RequestMapping("zhaopin_act_do")
    @ResponseBody
    @SaCheckLogin
    public SaResult zhaopin_act_do(@RequestBody Zhaopin zhaopin) {
        int suc = zhaopinDao.insertOrUpdateZhaopin(zhaopin);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }

    /**
     * 后台招聘删除
     * @param id
     * @return
     */
    @RequestMapping("zhaopin_del")
    @ResponseBody
    @SaCheckLogin
    public SaResult zhaopin_del(@RequestParam("id") Integer id) {
        int suc = zhaopinDao.deleteZhaopin(id);
        if (suc  == 1) {
            return SaResult.data(1);
        } else {
            return SaResult.error("更新失败");
        }
    }


}
